package com.woniu.woniuproject.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.woniuproject.entity.Debt_info;
import com.woniu.woniuproject.entity.LooseLabel;
import com.woniu.woniuproject.mapper.Debt_infoMapper;
import com.woniu.woniuproject.mapper.LooseLabelMapper;
import com.woniu.woniuproject.mapper.UserMapper;
import com.woniu.woniuproject.vo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.Date;
import java.util.List;

/**
 * 功能描述:<br>
 * 〈〉
 *
 * @author 徐威
 * @create 2020/11/16
 * @since 1.0.0
 */
@Service
public class LooseLabelService {
    @Autowired
    LooseLabelMapper looseLabelMapper;
    @Autowired
    UserMapper userMapper;
    /**
     * 散标分页查询
     */
    @Autowired
    Debt_infoMapper debt_infoMapper;
    public PageInfo<LooseLabel> queryByAplay(Date startDate, Date endDate, Page page,int status){
        PageHelper.startPage(page.getNowPage(),page.getMaxPage());
        List<LooseLabel> looseLabels = looseLabelMapper.selectByAplay(startDate, endDate,status);
        PageInfo<LooseLabel> info = new PageInfo<>(looseLabels);
        return info;
    }

    /**
     * 散标审核，审核通过后自动生成债权表
     * @param status
     * @param labelid
     * @return
     */
    @Transactional
    public int madifyStatusByid(int status, int labelid) {
        int i = looseLabelMapper.updateStatusByid(status, labelid);
        if (i>0&&status==1){
            Debt_info debt_info= new Debt_info();
            LooseLabel looseLabel = looseLabelMapper.selectBykey(labelid);
            debt_info.setDebtorsid(userMapper.selectByPrimaryKey(looseLabel.getDeborid()).getIdcart());
            debt_info.setDebtorsname(looseLabel.getDebtorname());
            debt_info.setLoanpurpose(looseLabel.getDebtoruser());
            debt_info.setLoantype(looseLabel.getLoanmode());
            debt_info.getRepaymentstyle(looseLabel.getRepaymentmethod());
            debt_info.setBorrowerid(looseLabel.getDeborid());
            debt_info.setAvailablemoney(looseLabel.getDebtormoney());
            debt_info.setMatchedstatus("11403");
            debt_info.setDebttype("散标");
            debt_info.setStartdate(new Date());
            debt_info.setLoanperiod(looseLabel.getDebormoney().toString());
            debt_info.setDabtrate(looseLabel.getIncomerate());
            debt_info.setDebttypename(looseLabel.getProjectnumber());
            debt_infoMapper.insert(debt_info);
        }
        return i;
    }

}
